<?php
// +----------------------------------------------------------------------
// | Author: huangzhen_20210406
// +----------------------------------------------------------------------

namespace app\common\model;
use think\facade\Db;

class CdApiAccept extends Common
{
    //---------------------------------------------后端---------------------------------------------------------------
    /**
     * 返回layui的table所需要的格式
     * @param $post
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public static function tableDataAdmin($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];
        if(isset($post['card_token']) && !empty($post['card_token'])){
            $where[] = ['at.card_token', '=', trim($post['card_token'])];
        }
        if(isset($post['tran_token']) && !empty($post['tran_token'])){
            $where[] = ['at.tran_token', '=', trim($post['tran_token'])];
        }
        if(isset($post['app_code']) && !empty($post['app_code'])){
            $where[] = ['at.app_code', '=', trim($post['app_code'])];
        }
        if(isset($post['datetime']) && !empty($post['datetime'])){
            $time_list = explode('_' , $post['datetime']);
            $start_time = strtotime($time_list[0] . " 00:00:00");
            $end_time   = strtotime($time_list[1] . " 23:59:59");
            $where[] = ['at.ctime', 'between', "{$start_time},{$end_time}"];
        }
        $list = Db::name('cd_api_accept')->alias('at')
            ->leftJoin('cards c' , 'c.card_token=at.card_token')
            ->field('at.id,c.memo,c.card_num,c.card_token,at.*')
            ->order('at.ctime desc')
            ->where($where)
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();

        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }

    /**
     * 返回layui的table所需要的格式
     * @param $post
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableDataOne($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];
        if(isset($post['tran_token']) && !empty($post['tran_token'])){
            $where[] = ['token', 'like', '%'.$post['tran_token'].'%'];
        }
        if(isset($post['datetime']) && !empty($post['datetime'])){
            $time_list = explode('_' , $post['datetime']);
            $start_time = strtotime($time_list[0] . " 00:00:00") - 8*60*60;
            $end_time   = strtotime($time_list[1] . " 23:59:59") - 8*60*60;
            $where[] = ['ctime', 'between', "{$start_time},{$end_time}"];
        }
        $db_name = 'api_tran_';
        if(isset($post['select_year']) && !empty($post['select_year'])){
            $db_name .= $post['select_year'];
        }else{
            $db_name .= getMonthTable(getUtcTime());
        }
        $list = Db::name($db_name)
            ->field('id,token,state,request,response,code,ctime')
            ->order('ctime desc')
            ->where($where)
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();

        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }
}